﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using System.Drawing.Imaging;
using System.IO;
using System.Data.OleDb;
using System.Data.SqlClient;
using DBBase;
using System.Xml;



namespace Business
{
    public partial class zhidongquxian : Form
    {
        public int iDETECT_ID;
        public int iVaxID;
        public string zzlz;
        public string zzly;
        public string gccz;
        public string gccy;
        public string shphm;
        public zhidongquxian(int iDET,int izhoushu,string szdz,string szdy,string sgcz,string sgcy,string hphm)
        {
            InitializeComponent();
            shphm = hphm;
            zzlz=szdz;
                zzly=szdy;
                gccz = sgcz;
                gccy = sgcy;
            iDETECT_ID = iDET;
            iVaxID = izhoushu;
            getshujuliu(iDETECT_ID, iVaxID);
        }
        public void getshujuliu(int iDET, int izhoushu)
        {
            string zdqx = string.Format("DET_BRIKEQX{0}", izhoushu);//左轮重
            byte[] content;
            int[] izdz=new int[1600];
            int[] izdy=new int[1600];
            int imax=0;
            Query mquery = new Query();
            string sql;
            sql = string.Format(@"select {0} from Detects_SJB Where DETECT_ID={1}",zdqx, iDET );
            DataTable dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][zdqx].ToString().Length > 10)
                {
                    content = (byte[])dt.Rows[0][zdqx];
                    int count = content.Length / 4;

                    for (int i = 0; i < count; i++)
                    {
                        izdz[i] = content[i * 4 + 0] + content[i * 4 + 1] * 256;

                        izdy[i] = content[i * 4 + 2] + content[i * 4 + 3] * 256;

                        if (imax < izdz[i])
                        {
                            imax = izdz[i];
                        }
                        if (imax < izdy[i])
                        {
                            imax = izdy[i];
                        }
                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(string.Format("{0}", izdz[i]));
                        lt.SubItems.Add(string.Format("{0}", izdy[i]));
                       
                        this.lv.Items.Add(lt);

                    }
                    WriteGraphEX(zg2, izdz, izdy, imax, iVaxID, content.Length / 2 > 300 ? 300 : content.Length / 2);
                }
                else
                {
                    MessageBox.Show("无曲线");
                }

            }
           
        }
        public void WriteGraphEX(ZedGraphControl zgc, int[] izdz, int[] izdy, double imax, int ivax, int icount)
        {
            GraphPane myPane = zgc.GraphPane;
            myPane.Fill = new Fill(Color.White, Color.Aqua, 45.0f);
            myPane.Legend.Fill = new Fill(Color.Gainsboro);
            myPane.Chart.Fill = new Fill(Color.Gainsboro);

            myPane.CurveList.Clear();

            myPane.Title.Text = string.Format("{0}第{1}轴制动曲线", shphm,ivax);




            //在面板上添加文本
            //TextObj text2 = new TextObj(
            //    "左制动力: 122\n右制动力: 2131\n过程差左差点: 12\n过程差右差点: 123",
            //    0.05f, 0.95f, CoordType.XPaneFractionYChartFraction, AlignH.Left, AlignV.Bottom);
            string sneirong = string.Format("左制动力:{0}\n右制动力:{1}\n过程差左差点:{2}\n过程差右差点:{3}", zzlz, zzly,gccz,gccy);
            TextObj text2 = new TextObj(sneirong,
               0.75f, 0.25f, CoordType.XPaneFractionYChartFraction, AlignH.Left, AlignV.Bottom);
            text2.FontSpec.StringAlignment = StringAlignment.Near;
            myPane.GraphObjList.Add(text2);
            int igccz = 0;
            int.TryParse(gccz, out igccz);
            bool bgcs = true;
            bool bzdz = true;
            bool bzdy = true;
            //添加箭头图案
            //ArrowObj myArrow = new ArrowObj(Color.Red, 12F, 230F, 70F, 280F, 55F);
            //myPane.GraphObjList.Add(myArrow);
       
      

            myPane.XAxis.Title.Text = "时间(10ms)";
            myPane.YAxis.Title.Text = "制动力(10xN)";

            myPane.YAxis.MajorGrid.IsVisible = true;
            myPane.YAxis.MinorTic.Size = 0f;

            myPane.YAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = imax + 30;


            myPane.YAxis.Scale.MajorStep = (imax + 30) / 10;



            zgc.IsShowPointValues = true;//数据节点
            string ss0 = string.Format("左制动");
            string ss1 = string.Format("右制动");

            PointPairList list0 = new PointPairList();
            PointPairList list1 = new PointPairList();

            PointPairList list2 = new PointPairList();

            PointPairList list3 = new PointPairList();
            PointPairList list4 = new PointPairList();


            LineItem myCurve0 = myPane.AddCurve(ss0, list0, Color.Blue,
                                    SymbolType.None);
            LineItem myCurve1 = myPane.AddCurve(ss1, list1, Color.Red,
                                    SymbolType.None);

            LineItem myCurve2 = myPane.AddCurve("过程差", list2, Color.Black,
                                   SymbolType.TriangleDown);



            LineItem myCurve3 = myPane.AddCurve("", list3, Color.Black,
                                  SymbolType.XCross);

            LineItem myCurve4 = myPane.AddCurve("", list4, Color.Black,
                                 SymbolType.XCross);


            myCurve0.Line.Width = 1.7F;
            myCurve1.Line.Width = 1.7F;

            myCurve2.Line.Width = 1.7F;

            myCurve3.Line.Width = 3.7F;
            myCurve4.Line.Width = 3.7F;

            for (int i = 0; i < icount; i++)
            {

                list0.Add(i, (double)izdz[i]);
                list1.Add(i, (double)izdy[i]);

                if (Math.Abs(izdz[i] - igccz) < 1 && bgcs)
                {
                    list2.Add(i, (double)izdz[i]);
                    list2.Add(i, (double)izdy[i]);
                    bgcs = false;
                }

                if (Math.Abs(izdz[i] - int.Parse(zzlz)) < 1 && bzdz)
                {
                    list3.Add(i, (double)izdz[i]);
                   // list3.Add(i+1, (double)izdz[i]);
                    bzdz = false;
                }

                if (Math.Abs(izdy[i] - int.Parse(zzly)) < 1 && bzdy)
                {
                    list4.Add(i, (double)izdy[i]);
                 //   list4.Add(i+1, (double)izdy[i]);
                    bzdy = false;
                }
               
            }
            zgc.AxisChange();
            zgc.Invalidate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = "";
            path = Application.StartupPath + path + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            excel(path);
        }
        private void excel(string path)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();
                // sb.Append("地区名" + "\t");
                sb.Append("制动过程数据\t");
                sb.Append(Environment.NewLine);
                for (int i = 0; i < lv.Columns.Count; i++)
                    sb.Append(lv.Columns[i].Text.ToString() + "\t");
                sb.Append(Environment.NewLine);

                foreach (ListViewItem li in lv.Items)
                {
                    for (int i = 0; i < lv.Columns.Count; i++)
                        sb.Append(li.SubItems[i].Text + "\t");
                    sb.Append(Environment.NewLine);
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("成功保存于" + path + "文件！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
     
    }
}
